package com.gouqi.mall.controller;

import com.gouqi.mall.domain.User;
import com.gouqi.mall.service.UserService;
import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@CrossOrigin
@Component
@Api(basePath = "/api/v1", value = "用户接口", description = "用户接口")
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController{

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/addUser",method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "创建一个用户",notes = "创建用户接口，返回创建状态",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "account",value = "用户账号",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "password",value = "用户密码",required = true,dataType = "String")
    })
    public String addUser(@RequestParam String account, @RequestParam  String password){
        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        System.out.println("用户："+ user.getAccount() + " 用户密码：" + user.getPassword() );
        JSONObject jsonObject = new JSONObject();
        Integer userId = userService.getUserIdByAccount(account);
        System.out.println("userId:"+userId);
        if (userId == null) {
            Integer result = userService.insertUser(user);
            if (result > 0) {
                jsonObject.put("status",200);
                jsonObject.put("message","新增一个用户！");
                jsonObject.put("result","success");
            } else {
                jsonObject.put("status",404);
                jsonObject.put("message","新增用户失败！");
                jsonObject.put("result","failed");
            }
        } else {
            user.setId(userId);
            Boolean result = userService.updateUserById(user);
            System.out.println("更新的结果："+result);
            if (result) {
                jsonObject.put("status",200);
                jsonObject.put("message","该用户已存在，更新用户成功！");
                jsonObject.put("result","success");
            } else {
                jsonObject.put("status",404);
                jsonObject.put("message","用户已存在，更新用户失败！");
                jsonObject.put("result","update Failed");
            }
        }
        return jsonObject.toString();
    }

    @ResponseBody
    @PostMapping(value = "/insertUser/{account}/{password}", produces = "application/json")
    @ApiOperation(value = "创建一个用户",notes = "创建用户接口，返回创建状态",httpMethod = "POST")
    public String insertUser(
            @ApiParam(required = true,name = "account",value = "用户账号")
            @PathVariable(value = "account")
            String account,
            @ApiParam(required = true,name = "password",value = "用户密码")
            @PathVariable(value = "password")
            String password)
    {

        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        JSONObject jsonObject = new JSONObject();
        Integer userId = userService.getUserIdByAccount(account);
        System.out.println("获取用户ID:"+userId);
        if (userId == null) {
            System.out.println("获取用户ID不为空");
            Integer result = userService.insertUser(user);
            System.out.println("新增用户结果："+result);
            if (result > 0) {
                jsonObject.put("status",200);
                jsonObject.put("message","新增用户成功！");
                jsonObject.put("result","success");
            } else {
                jsonObject.put("status",404);
                jsonObject.put("message","新增用户失败！");
                jsonObject.put("result","failed");
            }
        } else {
            System.out.println("该用户是老用户");
            user.setId(userId);
            Boolean result = userService.updateUserById(user);
            System.out.println("更新老用户的信息："+result);
            if (result) {
                jsonObject.put("status",200);
                jsonObject.put("message","用户已经存在，更新用户成功！");
                jsonObject.put("result","success");
            } else {
                jsonObject.put("status",404);
                jsonObject.put("message","用户已经存在，更新用户失败！");
                jsonObject.put("result","update Failed");
            }
        }
        return jsonObject.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserDetails.do",method = RequestMethod.GET,produces = "application/json")
    @ApiOperation(value = "获取用户信息详情",notes = "获取用户个人信息详情",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "token",value = "用户token",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "userId",value = "用户id",required = true,dataType = "String")
    })
    public String queryUserDetails(@RequestParam(name = "userId") String userId, HttpServletRequest request){
        return userService.queryUserDetails(userId);
    }

}
